/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.fspike.gui.taxonomytree.rendering;

import com.compomics.fspike.gui.taxonomytree.NCBITaxonomyTree;
import java.awt.Color;
import java.awt.Component;
import java.awt.Toolkit;
import java.util.Hashtable;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *
 * @author Kenneth
 */
public class TaxonomyTreeIconRenderer extends DefaultTreeCellRenderer {

    private final Icon error = (new ImageIcon(Toolkit.getDefaultToolkit().getImage(TaxonomyTreeIconRenderer.class.getResource("/icons/Error_3.png"))));
    private final Icon accepted = new ImageIcon(Toolkit.getDefaultToolkit().getImage(TaxonomyTreeIconRenderer.class.getResource("/icons/accept.png")));
    private final Icon next = (new ImageIcon(Toolkit.getDefaultToolkit().getImage(TaxonomyTreeIconRenderer.class.getResource("/icons/next.png"))));
    private final Icon next_grey = new ImageIcon(Toolkit.getDefaultToolkit().getImage(TaxonomyTreeIconRenderer.class.getResource("/icons/next_grey.png")));
    private final Icon endpoint = (new ImageIcon(Toolkit.getDefaultToolkit().getImage(TaxonomyTreeIconRenderer.class.getResource("/icons/close.png"))));
    private final Icon endpoint_grey = new ImageIcon(Toolkit.getDefaultToolkit().getImage(TaxonomyTreeIconRenderer.class.getResource("/icons/close_grey.png")));
    private final Icon visited = (new ImageIcon(Toolkit.getDefaultToolkit().getImage(TaxonomyTreeIconRenderer.class.getResource("/icons/help_no_frame.png"))));
    private final Icon visited_grey = new ImageIcon(Toolkit.getDefaultToolkit().getImage(TaxonomyTreeIconRenderer.class.getResource("/icons/help_no_frame_grey.png")));

    public TaxonomyTreeIconRenderer(NCBITaxonomyTree taxonomyTree) {
        super();
        TaxonomyTreeIconFactory.getInstance().makeIcons(taxonomyTree);
        setBorderSelectionColor(Color.DARK_GRAY);
        setTextSelectionColor(Color.BLACK);
        setTextNonSelectionColor(Color.DARK_GRAY);
        setBackgroundSelectionColor(Color.LIGHT_GRAY);
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean sel, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
                row, hasFocus);
        if (value instanceof DefaultTaxonomyTreeNode) {
            DefaultTaxonomyTreeNode node = (DefaultTaxonomyTreeNode) value;
            Icon icon = node.getIcon();
            if (icon == null) {
                Hashtable icons = (Hashtable) tree.getClientProperty("JTree.icons");
                String name = ((DefaultTaxonomyTreeNode) value).getIconName();
                if ((icons != null) && (name != null)) {
                    icon = (Icon) TaxonomyTreeIconFactory.getInstance().getIcon(name);
                }
            }
            if (sel) {
                if (node.wasExplored()) {
                    if (node.hasChildren()) {
                        setIcon(next);
                    } else {
                        setIcon(endpoint);
                    }
                } else {
                    setIcon(visited);
                }
            } else {
                if (node.wasExplored()) {
                    if (node.hasChildren()) {
                        setIcon(next_grey);
                    } else {
                        setIcon(endpoint_grey);
                    }
                } else {
                    setIcon(visited_grey);
                }
            }
        } else {
            return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        }
        return this;
    }
}
